{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "qSXf2GQUPK0w"
   },
   "source": [
    "# Running Code"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "FqvLtG2rPK0y"
   },
   "source": [
    "First and foremost, the Jupyter Notebook is an interactive environment for writing and running code. The notebook is capable of running code in a wide range of languages. However, each notebook is associated with a single kernel.  This notebook is associated with the IPython kernel, therefore runs Python code."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "o_t3ZsX4PK0z"
   },
   "source": [
    "## Code cells allow you to enter and run code"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "vZmdEX3DPK00"
   },
   "source": [
    "Run a code cell using `Shift-Enter` or pressing the <button class='btn btn-default btn-xs'><i class=\"icon-step-forward fa fa-step-forward\"></i></button> button in the toolbar above:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 0,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "BpRuIUGHPK01"
   },
   "outputs": [],
   "source": [
    "a = 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 34
    },
    "colab_type": "code",
    "id": "fnpf46qBPK05",
    "outputId": "ac454578-c886-4b04-9de4-8001a8b26577"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n"
     ]
    }
   ],
   "source": [
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "fimFb-WJPK1A"
   },
   "source": [
    "There are two other keyboard shortcuts for running code:\n",
    "\n",
    "* `Alt-Enter` runs the current cell and inserts a new one below.\n",
    "* `Ctrl-Enter` run the current cell and enters command mode."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "_nWwBZOmPK1B"
   },
   "source": [
    "## Managing the Kernel"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "pnSA9-_RPK1C"
   },
   "source": [
    "Code is run in a separate process called the Kernel.  The Kernel can be interrupted or restarted.  Try running the following cell and then hit the <button class='btn btn-default btn-xs'><i class='icon-stop fa fa-stop'></i></button> button in the toolbar above."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 0,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "pRojUUXpPK1C"
   },
   "outputs": [],
   "source": [
    "import time\n",
    "time.sleep(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "CpBc_I_oPK1G"
   },
   "source": [
    "If the Kernel dies you will be prompted to restart it. Here we call the low-level system libc.time routine with the wrong argument via\n",
    "ctypes to segfault the Python interpreter:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 0,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "Hb219SSWPK1H"
   },
   "outputs": [],
   "source": [
    "import sys\n",
    "from ctypes import CDLL\n",
    "# This will crash a Linux or Mac system\n",
    "# equivalent calls can be made on Windows\n",
    "\n",
    "# Uncomment these lines if you would like to see the segfault\n",
    "\n",
    "# dll = 'dylib' if sys.platform == 'darwin' else 'so.6'\n",
    "# libc = CDLL(\"libc.%s\" % dll) \n",
    "# libc.time(-1)  # BOOM!!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "ctxjxwoLPK1J"
   },
   "source": [
    "## Cell menu"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "0z5jISIvPK1L"
   },
   "source": [
    "The \"Cell\" menu has a number of menu items for running code in different ways. These includes:\n",
    "\n",
    "* Run and Select Below\n",
    "* Run and Insert Below\n",
    "* Run All\n",
    "* Run All Above\n",
    "* Run All Below"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "dT1ty5vnPK1M"
   },
   "source": [
    "## Restarting the kernels"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "dGvq4AGoPK1N"
   },
   "source": [
    "The kernel maintains the state of a notebook's computations. You can reset this state by restarting the kernel. This is done by clicking on the <button class='btn btn-default btn-xs'><i class='fa fa-repeat icon-repeat'></i></button> in the toolbar above."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "Ffr2YBCEPK1O"
   },
   "source": [
    "## sys.stdout and sys.stderr"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "3asl6eWjPK1P"
   },
   "source": [
    "The stdout and stderr streams are displayed as text in the output area."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 34
    },
    "colab_type": "code",
    "id": "j9wH8R9EPK1Q",
    "outputId": "cc271283-bae4-4385-d418-04741e75d7f3"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hi, stdout\n"
     ]
    }
   ],
   "source": [
    "print(\"hi, stdout\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 34
    },
    "colab_type": "code",
    "id": "LclWlgwbPK1U",
    "outputId": "ab0e8ef1-33a8-49ec-ac96-9ea834dc60b7"
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "hi, stderr\n"
     ]
    }
   ],
   "source": [
    "print('hi, stderr', file=sys.stderr)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "5QBmFoIGPK1Y"
   },
   "source": [
    "## Output is asynchronous"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "Qh1_P0UGPK1Y"
   },
   "source": [
    "All output is displayed asynchronously as it is generated in the Kernel. If you execute the next cell, you will see the output one piece at a time, not all at the end."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 153
    },
    "colab_type": "code",
    "id": "FKupEea9PK1a",
    "outputId": "c0bf86ed-bf12-4dc8-f016-6dc1c24a4e5b"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n"
     ]
    }
   ],
   "source": [
    "import time, sys\n",
    "for i in range(8):\n",
    "    print(i)\n",
    "    time.sleep(0.5)"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "name": "test.ipynb",
   "version": "0.3.2",
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
